package com.banciyuan.bcywebview.biz.setting.bansetting;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.imageview.CircleImageView;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.biz.share.block.BlockHelper;
import com.banciyuan.bcywebview.biz.share.block.IBlockHelper;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Role;
import de.greenrobot.daoexample.model.UserDetail;

/**
 * Created by squall on 2016/3/7.
 */
public class BlockListAdapter extends BaseAdapter {

    private Context mContext;
    private List<UserDetail> ulist = new ArrayList<UserDetail>();

    public BlockListAdapter(Context mContext, List<UserDetail> ulist) {
        this.mContext = mContext;
        this.ulist = ulist;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 1 : 0;
    }

    @Override
    public int getCount() {
        return ulist.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return ulist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        public CircleImageView civTouXiang;
        public TextView tvGuanzhuName;
        public TextView tvCoser;
        public TextView tvDrawer;
        public TextView tvWriter;
        public TextView ivRemove;


        public ViewHolder(View convertView) {
            civTouXiang = (CircleImageView) convertView.findViewById(R.id.civ_touxiang);
            tvGuanzhuName = (TextView) convertView.findViewById(R.id.tv_guanzhu_name);
            tvCoser = (TextView) convertView.findViewById(R.id.tv_coser);
            tvDrawer = (TextView) convertView.findViewById(R.id.tv_drawer);
            tvWriter = (TextView) convertView.findViewById(R.id.tv_writer);
            ivRemove = (TextView) convertView.findViewById(R.id.tv_remove);
        }
    }

    private class ViewEmpty {
        private LinearLayout block_empty;
        private ImageView imageView;

        private ViewEmpty(View convertView) {
            block_empty = (LinearLayout) convertView.findViewById(R.id.block_empty);
            imageView = (ImageView) convertView.findViewById(R.id.empty_img);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewEmpty viewEmpty = null;
        if (null == convertView) {
            if (getItemViewType(position) == 0) {
                convertView = View.inflate(mContext, R.layout.block_item, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                convertView = View.inflate(mContext, R.layout.block_empty, null);
                viewEmpty = new ViewEmpty(convertView);
                convertView.setTag(viewEmpty);
            }

        } else {
            if (getItemViewType(position) == 0) {
                viewHolder = (ViewHolder) convertView.getTag();
            } else {
                viewEmpty = (ViewEmpty) convertView.getTag();
            }
        }
        if (getItemViewType(position) == 0 && viewHolder != null) {
            final UserDetail userDetaill = ulist.get(position - 1);
            ImageLoader.getInstance().displayImage(userDetaill.getAvatar(), viewHolder.civTouXiang, ImageOptions.optionHead);
            viewHolder.tvGuanzhuName.setText(userDetaill.getUname());
            List<Role> roles = userDetaill.getRoles();
            viewHolder.tvCoser.setVisibility(View.GONE);
            viewHolder.tvDrawer.setVisibility(View.GONE);
            viewHolder.tvWriter.setVisibility(View.GONE);
            if (null != roles && !roles.isEmpty()) {
                for (int i = 0; i < roles.size(); i++) {
                    String type_name = roles.get(i).getType_name();
                    if (mContext.getString(R.string.coser_role).equals(type_name)) {
                        if (roles.size() < 3) {
                            viewHolder.tvCoser.setText(mContext.getString(R.string.coser_role));
                        } else {
                            viewHolder.tvCoser.setText(mContext.getString(R.string.coser_role) + "  /");
                        }
                        viewHolder.tvCoser.setVisibility(View.VISIBLE);
                    }
                    if (mContext.getString(R.string.draw_role).equals(type_name)) {
                        if (roles.size() == 1) {
                            viewHolder.tvDrawer.setText(mContext.getString(R.string.draw_role));
                        } else if (roles.size() > 1) {
                            viewHolder.tvDrawer.setText(mContext.getString(R.string.draw_role) + "  /");
                        }
                        viewHolder.tvDrawer.setVisibility(View.VISIBLE);
                    }
                    if (mContext.getString(R.string.write_role).equals(type_name)) {
                        viewHolder.tvWriter.setText(mContext.getString(R.string.write_role));
                        viewHolder.tvWriter.setVisibility(View.VISIBLE);
                    }
                }
            }

            ((View) (viewHolder.civTouXiang.getParent())).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, userDetaill.getUid());
                }
            });

            viewHolder.ivRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IBlockHelper iBlockHelper = new IBlockHelper() {
                        @Override
                        public void getSucc(String json) {
                            if (position - 1 < ulist.size() && position - 1 >= 0) {
                                ulist.remove(position - 1);
                            }
                            notifyDataSetChanged();
                        }

                        @Override
                        public void getFail() {
                            MyToast.show(mContext, mContext.getString(R.string.operation_fail));
                        }
                    };
                    BlockHelper blockHelper = new BlockHelper(iBlockHelper, mContext);
                    blockHelper.unBlock(UserDataHelper.getInstance(mContext).getToken(), userDetaill.getUid());
                }
            });
        } else {
            if (viewEmpty != null) {
                if (ulist.isEmpty()) {
                    viewEmpty.block_empty.setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().displayImage(mContext.getString(R.string.block_user_empty_guide_image), viewEmpty.imageView, ImageOptions.optionsCopy);
                } else {
                    viewEmpty.block_empty.setVisibility(View.GONE);
                }
            }
        }

        return convertView;
    }
}
